package com.example.word_book.service;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.word_book.R;
import com.example.word_book.bean.Lexicon;
import com.example.word_book.bean.Word;
import com.example.word_book.utils.SqlHelp;

public class AddWordActivity extends AppCompatActivity {

    private int chooseId = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_word);
        init();
    }

    private void init() {
        ImageView back = findViewById(R.id.img_back);
        EditText edt_word = findViewById(R.id.edt_word);
        EditText edt_translation = findViewById(R.id.edt_translation);
        TextView tv_type = findViewById(R.id.tv_type);
        Button btn_add = findViewById(R.id.btn_add);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        edt_word.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String s = edt_word.getText().toString().trim();
                if (!"".equals(s)) {
                    for (int k = 0; k < s.length(); k++) {
                        String end = s.substring(k, k + 1).toUpperCase();
                        Log.i(end, "onTextChanged: ");
                        if (!(end.compareTo("A") >= 0 && end.compareTo("Z") <= 0)) {
                            edt_word.setText(s.replace(s.substring(k, k + 1),""));
                        }
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        tv_type.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new AlertDialog.Builder(AddWordActivity.this)
                        .setTitle("请选择")
                        .setSingleChoiceItems(Lexicon.TYPE, chooseId, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                chooseId = i;
                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                tv_type.setText(Lexicon.TYPE[chooseId]);
                            }
                        })
                        .show();
            }
        });

        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SqlHelp sqlHelp = new SqlHelp(AddWordActivity.this);
                sqlHelp.insertWord(new Word(edt_word.getText().toString(), tv_type.getText().toString(), edt_translation.getText().toString()));
                Toast.makeText(AddWordActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
                edt_word.setText("");
                edt_translation.setText("");
            }
        });
    }
}